# import pandas as pd

# # 读取xlsx文件
# df = pd.read_excel('G:\data.xlsx')

# # 将数据帧写入csv文件
# df.to_csv('G:\data.csv', index=False)
# import pandas as pd
# import random

# # 读取csv文件，第一行作为表头
# df = pd.read_csv('G:\data.csv', header=0)

# # 定义函数来根据身份证的第17位确定性别
# def get_gender(id_number):
#     id_number = id_number[0]  # 只使用第17位
#     if int(id_number) % 2 == 0:
#         return '女'
#     else:
#         return '男'

# # 定义函数来生成随机姓名
# def get_name():
#     surnames = ["张", "王", "陈", "左", "蒋", "于", "李", "董", "林", "韩", "叶"]
#     given_names = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
#     return random.choice(surnames) + random.choice(given_names)

# # 对每一行应用函数，生成姓名和性别列
# df["姓名"] = [get_name() for _ in range(1, len(df) + 1)]
# df["性别"] = df["身份证第十七位"].apply(get_gender)

# # 保存到新的csv文件
# df.to_csv('G:/new_data.csv', index=False)


import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('G:/new_data.csv', header = 0)

units = {
    "珠海路派出所": "Zhuhai Road Police Station",
    "特巡警大队": "Special Patrol Police Team",
    "治安大队": "Public Security Team",
    "指挥中心": "Command Center",
    "界石派出所": "Jieshi Police Station",
    "南海综指": "Nanhai Comprehensive Direction",
    "天福派出所": "Tianfu Police Station",
    "环山派出所": "Huan Shan Police Station",
    "张家产派出所": "Zhangjiachan Police Station",
    "米山派出所": "Mishan Police Station",
    "文登营派出所": "Wendengying Police Station",
    "泽头派出所": "Zetou Police Station",
    "龙山派出所": "Longshan Police Station",
    "五垒岛派出所": "Wulei Island Police Station",
    "小观派出所": "Xiaoguan Police Station",
    "纪委": "Disciplinary Committee",
    "经侦大队": "Economic Investigation Team",
    "警务保障室分局餐厅乐康": "Police Support Department Sub-Bureau Cafeteria Lekang",
    "海岸大队": "Coastal Team",
    "食药环侦大队": "Food and Drug Environmental Investigation Team",
    "南海综指餐厅乐康": "Nanhai Comprehensive Direction Cafeteria Lekang",
    "埠口派出所": "Bukou Police Station",
    "高村派出所": "Gao Village Police Station",
    "宋村派出所": "Song Village Police Station",
    "警务保障室": "Police Support Department",
    "刑侦大队": "Criminal Investigation Team",
    "葛家派出所": "Gejia Police Station",
    "大水泊派出所": "Dashuibo Police Station",
    "网安大队": "Cybersecurity Team",
    "侯家派出所": "Houjia Police Station",
    "警务保障室分局物业": "Police Support Department Sub-Bureau Property",
    "户口管理科": "Household Management Department",
    "宣传": "Publicity",
    "南海新区派出所": "Nanhai New District Police Station",
    "局党委": "Bureau Party Committee",
    "政工": "Political Work",
    "政保大队": "Political Security Team",
    "出入境管理大队": "Entry-Exit Management Team",
    "禁毒大队": "Drug Prohibition Team",
    "法制大队": "Legal System Team",
    "控申部门": "Control Application Department",
    "通信科": "Communication Department",
    "森警大队": "Forest Police Team",
    "特巡警餐厅乐康": "Special Patrol Police Cafeteria Lekang",
    "警务保障室特巡警物业": "Police Support Department Special Patrol Property",
    "特巡警大队餐厅乐康": "Special Patrol Police Team Cafeteria Lekang"
}


# 从出生日期中提取出年份
df['Birthdate'] = pd.to_datetime(df['Birthdate'], format='%Y%m%d', errors='coerce').dt.year

fig, axs = plt.subplots(2, 2, figsize=(15,10))  # 创建 2x2 的 subplot 布局

# 柱状图可视化性别、岗位和单位
for i, column in enumerate(['Gender', 'Post', 'Unit']):
    value_counts = df[column].value_counts()
    axs[i//2, i%2].bar(value_counts.index, value_counts.values)
    axs[i//2, i%2].set_xlabel(column)
    axs[i//2, i%2].set_ylabel('Count')
    axs[i//2, i%2].set_title('Distribution of ' + column)
    for tick in axs[i//2, i%2].get_xticklabels():
        tick.set_rotation(45)

# 直方图可视化出生年份
axs[1, 1].hist(df['Birthdate'], bins=20, edgecolor='black')
axs[1, 1].set_xlabel('Year of Birth')
axs[1, 1].set_ylabel('Count')
axs[1, 1].set_title('Distribution of Year of Birth')

plt.tight_layout()
plt.show()

